package com.ljy.my_study.有效的括号;

import java.util.LinkedList;
import java.util.List;

/** 
* @author James
* @date 2019年5月3日 
*/
public class TestMain2 {
	
	public static void main(String[] args) {
		System.out.println(isValid("{{({})}}"));
	}
	
	public static boolean isValid(String str) {
		List<Character> stack=new LinkedList<>();
		int i=0;
 		while(i<str.length()) {
			char c=str.charAt(i);
			if(i!=0) {
				char beforeC=stack.get(stack.size()-1);
				i++;
				if((beforeC=='{'&&c=='}')
						||(beforeC=='('&&c==')')
						||(beforeC=='['&&c==']')) {
					stack.remove(stack.size()-1);
				}else if(c=='('||c=='['||c=='{') {
					stack.add(c);
					continue;
				}else {
					return false;
				}
			}else {
				stack.add(c);
				i++;
			}
		}
		if(stack.size()==0) {
			return true;
		}else {
			return false;
		}
	}
}
